var restify= require('restify')
,app= restify.createServer()
,config=require('../config')
,path= require('path')
,users = require('./users')
,weixin = require('./weixin')
,zsh = require('./zsh')
,admin = require('./admin')

app.use(restify.authorizationParser());
app.use(restify.CORS())
app.use(restify.queryParser())
app.use(restify.jsonp());
app.use(restify.bodyParser());
app.use(function(req,res,next){
	console.log(req.url)
	next()
})

app.get('/admin', function(req, res) {
	/*require('jsdom').env('public/books.txt', function(errors, window) {//需要nodejs v0.11以上版本支持
		var $ = require('jquery')(window)
		console.log(111,$("wang").children().text())
	})*/
	if(req.headers.userid===undefined&&req.method=='GET'){
		var data = require('fs').readFileSync(path.join(__dirname,'..','public', 'admin.html'));
		  res.status(200);
		  res.header('Content-Type', 'text/html');
		  return res.end(data.toString());
	}else next()
})

app.post('/admin/tree',admin.tree)
app.put('/admin/tree',admin.treePut)

app.get(/^.*\.(html|png|svg|css|js|ico|gif|xml|txt)$/,restify.serveStatic({directory: path.join(__dirname,'..','public')}))

app.use(function(req, res,next){
	var ua = req.headers['user-agent'],detector=require('detector')
	if(req.headers.userid===undefined&&req.method=='GET'){
		var data = require('fs').readFileSync(path.join(__dirname,'..','public', 'index.html'));
		  res.status(200);
		  res.header('Content-Type', 'text/html');
		  return res.end(data.toString());
	}else next()
})

app.get('/', function (req, res, next){
	res.end('11');
})
app.get('/aa/bb/cc', function (req, res, next){
	console.log(req.url);
})

app.get('/stream', function(req, res){
    res.writeHead(200, {"Content-Type":"text/event-stream"});
    res.write("event: connect\n");

    var interval = setInterval(function() {
		console.log(new Date())
		res.write("data: " + (new Date()) + "\n\n");
    }, 1000);
	
    req.connection.addListener("close", function () {
    	console.log('Connection closed!')
      clearInterval(interval);
    }, false);

})
app.get('/home',function(req,res){res.end(req.url)})
app.get('/weixin_token',weixin.token)
app.get('/zsh_mrzc',zsh.mrzc)

app.get('/login',users.loginView)
app.post('/login',users.loginAction)

app.on('NotFound', function(req, res) {
	console.log(req.url,'not found')
	if(req.headers.userid===undefined&&req.method=='GET'){
		var data = require('fs').readFileSync(path.join(__dirname,'..','public', 'index.html'));
		  res.status(200);
		  res.header('Content-Type', 'text/html');
		  return res.end(data.toString());
	}
	res.end('404')
})
/*app.on('after', restify.auditLogger({
	log : require('bunyan').createLogger({
		name : 'audit',
		stream : process.stdout
	})
}))*/
/*app.on('uncaughtException', function(req, res, route, error) {
	console.log({
		err : error,
		url : req.url,
		route : route,
		params : req.params
	});
	res.send(error)
})*/

app.listen(process.env.OPENSHIFT_NODEJS_PORT||config.port||81,process.env.OPENSHIFT_NODEJS_IP||config.host||'127.0.0.1',function(){
	console.error('Server started on %s:%s',process.env.OPENSHIFT_NODEJS_IP||config.host||'127.0.0.1',process.env.OPENSHIFT_NODEJS_PORT||config.port||81)
})
process.on('uncaughtException', function(e){console.error(e, 'Uncaught Exception')})